/*
 * Background Flickr: Copyright (C) 2009  Craig McNicholas
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.ddw.app.bflickr.data;

import java.util.Vector;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.ddw.app.bflickr.flickr.api.FlickrAccount;
import com.ddw.app.bflickr.flickr.api.utils.EmailAddress;

/**
 * The accounts database adapter.
 * @author craig
 * 
 */
public class AccountDatabase extends DatabaseAdapter {

	/**
	 * The databases name
	 */
	public final static String DATABASE_NAME = "bflickr_accounts";
	
	/**
	 * The database version
	 */
	public final static int DATABASE_VERSION = 1;
	
	/**
	 * The SQL statement used to create this database
	 */
	public final static String DATABASE_CREATE = "CREATE TABLE account (nsid TEXT PRIMARY KEY NOT NULL, username TEXT, email TEXT);";
	
	/**
	 * The SQL statement used to destroy this database
	 */
	public final static String DATABASE_DESTROY = "DROP TABLE IF EXISTS account";
	
	/**
	 * The account table name
	 */
	public final static String DATABASE_TABLE_ACCOUNT = "account";
	
	/**
	 * The nsid field name in the account table
	 */
	public final static String DATABASE_FIELD_ACCOUNT_NSID = "nsid";
	
	/**
	 * The email address field name in the account table
	 */
	public final static String DATABASE_FIELD_ACCOUNT_EMAIL = "email";
	
	/**
	 * The username field name in the account table
	 */
	public final static String DATABASE_FIELD_ACCOUNT_USERNAME = "username";

	/**
	 * Constructs a new account database adapter
	 * @param context the context of the application
	 */
	public AccountDatabase(Context context) {
		super(context, DATABASE_NAME, DATABASE_VERSION, DATABASE_CREATE, DATABASE_DESTROY);
	}
	
	/**
	 * Creates a new account record in the database
	 * @param account the flickr account to add to the database
	 * @return a flag indicating this methods success
	 */
	public boolean insertAccount(FlickrAccount account) {
		ContentValues values = new ContentValues();
		values.put(DATABASE_FIELD_ACCOUNT_NSID, account.getNSID());
		if(account.getUsername() != null) {
			values.put(DATABASE_FIELD_ACCOUNT_USERNAME, account.getUsername());
		}
		if(account.getEmailAddress() != null) {
			values.put(DATABASE_FIELD_ACCOUNT_EMAIL, account.getEmailAddress().getEmailAddress());
		}
		return super.getDatabase().insert(DATABASE_TABLE_ACCOUNT, null, values) != -1;
	}
	
	/**
	 * Updates an account record in the database
	 * @param account the flickr account to update
	 * @return a flag indicating this methods success
	 */
	public boolean updateAccount(FlickrAccount account) {
		ContentValues values = new ContentValues();
		values.put(DATABASE_FIELD_ACCOUNT_NSID, account.getNSID());
		if(account.getUsername() != null) {
			values.put(DATABASE_FIELD_ACCOUNT_USERNAME, account.getUsername());
		}
		if(account.getEmailAddress() != null) {
			values.put(DATABASE_FIELD_ACCOUNT_EMAIL, account.getEmailAddress().getEmailAddress());
		}
		String where = DATABASE_FIELD_ACCOUNT_NSID + "=" + account.getNSID();
		return super.getDatabase().update(DATABASE_TABLE_ACCOUNT, values, where, null) > 0;
	}
	
	/**
	 * Deletes a flickr account by its nsid
	 * @param nsid the flickr nsid to delete
	 * @return a flag indicating this methods success
	 */
	public boolean deleteAccount(String nsid) {
		String where = DATABASE_FIELD_ACCOUNT_NSID + "=" + nsid;
		return super.getDatabase().delete(DATABASE_TABLE_ACCOUNT, where, null) > 0;
	}
	
	/**
	 * Clears all the accounts from the database
	 * @return a flag indicating this methods success
	 */
	public boolean clearAccounts() {
		return super.getDatabase().delete(DATABASE_TABLE_ACCOUNT, "1", null) > 0;
	}
	
	/**
	 * Gets a flickr account object from the database from its nsid
	 * @param nsid the nsid of the account to retrieve
	 * @return a flickr account object or <u>null</u> if the record does not exist
	 */
	public FlickrAccount getAccount(String nsid) {
		String columns[] = new String[] { DATABASE_FIELD_ACCOUNT_NSID, DATABASE_FIELD_ACCOUNT_USERNAME, DATABASE_FIELD_ACCOUNT_EMAIL };
		String where = DATABASE_FIELD_ACCOUNT_NSID + "=" + nsid;
		Cursor cursor = super.getDatabase().query(DATABASE_TABLE_ACCOUNT, columns, where, null, null, null, DATABASE_FIELD_ACCOUNT_USERNAME);
		if(cursor != null) {
			cursor.moveToFirst();
			String username = cursor.getString(1);
			String email = cursor.getString(2);
			FlickrAccount account = new FlickrAccount(nsid);
			if(username != null) {
				account.setUsername(username);
			}
			if(email != null) {
				account.setEmailAddress(new EmailAddress(email));
			}
			return account;
		}
		return null;
	}
	
	/**
	 * Gets all the flickr accounts in the database
	 * @return a vector of flickr accounts in the database
	 */
	public Vector<FlickrAccount> getAccounts() {
		Vector<FlickrAccount> accounts = new Vector<FlickrAccount>();
		String columns[] = new String[] { DATABASE_FIELD_ACCOUNT_NSID, DATABASE_FIELD_ACCOUNT_USERNAME, DATABASE_FIELD_ACCOUNT_EMAIL };
		Cursor cursor = super.getDatabase().query(DATABASE_TABLE_ACCOUNT, columns, null, null, null, null, DATABASE_FIELD_ACCOUNT_USERNAME);
		if(cursor != null) {
			cursor.moveToFirst();
			while(!cursor.isAfterLast()) {
				String NSID = cursor.getString(0);
				String username = cursor.getString(1);
				String email = cursor.getString(2);
				FlickrAccount account = new FlickrAccount(NSID);
				if(username != null) {
					account.setUsername(username);
				}
				if(email != null) {
					account.setEmailAddress(new EmailAddress(email));
				}
				accounts.add(account);
				cursor.moveToNext();
			}
		}
		return accounts;
	}

}
